package best.driver.android.Utils;

import android.content.Context;
import android.os.PowerManager;

public class WakeLookUtil {
    private static PowerManager.WakeLock sWakeLock;

    @SuppressWarnings("deprecation")
    private synchronized static PowerManager.WakeLock getWakeLock(Context context) {
        if (sWakeLock == null) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);// 获取电源管理器对象
            int flag = PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP;
            sWakeLock = pm.newWakeLock(flag, "");
        }
        return sWakeLock;
    }

    public static void wakeUpScreen(Context context) {
        try {
            getWakeLock(context).acquire();// 点亮屏幕
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void releaseWakeLock(Context context) {
        try {
            // getWakeLock(context).setReferenceCounted(false);
            getWakeLock(context).release();// 释放
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
